//
//  ListTableViewCell.m
//  Civacrm
//
//  Created by 王桂敏 on 2017/2/15.
//  Copyright © 2017年 王桂敏. All rights reserved.
//

#import "ListTableViewCell.h"

@interface  ListTableViewCell()

@property (nonatomic,strong) NSArray *imageArray;

@property (nonatomic,strong) NSArray *changeImageArray;

@end
@implementation ListTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initData];
        [self initView];
    }
    return self;
}
- (void)initData{
    self.imageArray = @[@"icon_jinpai",@"icon_yinpai",@"icon_tongpai"];
    self.changeImageArray = @[@"shangsheng",@"xiajiang",@"chiping"];
}
- (void)initView{
    self.backgroundColor = WhiteColor;

    self.numberButton = [UIButton buttonOfTitleForNormalWithTitle:nil frame:CGRectMake(w(26), 0, w(56), h(75)) target:nil action:nil titleColor:ColorWithHexString(@"4e778f") backgroudColor:ClearColor font:font(26)];
    self.numberButton.centerY = h(140) / 2;
    [self.numberButton setTitleColor:ColorWithHexString(@"4e778f") forState:UIControlStateNormal];
    self.numberButton.titleLabel.font = font(36);
    [self addSubview:self.numberButton];
    
    self.iconImageView = [UIImageView imageViewWithFrame:CGRectMake(getMaxX(self.numberButton) + w(14), 0, w(88), h(88)) image:nil];
    self.iconImageView.layer.masksToBounds = YES;
    self.iconImageView.layer.cornerRadius = self.iconImageView.width / 2;
    self.iconImageView.centerY = h(140) / 2;
    [self addSubview:self.iconImageView];
    
    self.numberLabel = [UILabel labelWithTitle:nil frame:CGRectMake(kScreenWidth - w(18) - w(100), h(54), w(120), h(29)) font:font(29) textColor:ColorWithHexString(@"4e778f") bgColor:WhiteColor];
    [self addSubview:self.numberLabel];
    
    self.changeButton = [UIButton buttonOfHorizontal1ForNormalWithImage:@"shangsheng" title:@"0人" frame:CGRectMake(self.numberLabel.x + w(10), getMaxY(self.numberLabel) + h(16), self.numberLabel.width - w(20), h(20)) target:nil action:nil titleColor:ColorWithHexString(@"6ede6e") backgroudColor:WhiteColor font:font(16)];
    [self addSubview:self.changeButton];
    
    self.lineLabel = [[UILabel alloc] init];
    self.lineLabel.backgroundColor = GlobalGray;
    [self addSubview:self.lineLabel];
   
    
}
- (void)configCellWithModel:(ListModel *)model{
    if (self.nameLabel) {
        [self.nameLabel removeFromSuperview];
        self.nameLabel = nil;
    }
    
    if (model.type != ListModelType3) {
        [self.numberButton setImage:Image(self.imageArray[model.type]) forState:UIControlStateNormal];
        [self.numberButton setTitle:nil forState:UIControlStateNormal];
    } else {
        [self.numberButton setTitle:[NSString stringWithFormat:@"%@",@(model.number)] forState:UIControlStateNormal];
        [self.numberButton setImage:nil forState:UIControlStateNormal];
    }
    
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.picUrl] placeholderImage:Image(@"xiaotouxiang_kong")];
    
    self.nameLabel = [[RollingLabel alloc] initWithFrame:CGRectMake(getMaxX(self.iconImageView) + w(18), 0, w(315), h(140))];

    [self addSubview:self.nameLabel];
    [self.nameLabel setText:model.orgName];
    
    self.numberLabel.text = [NSString stringWithFormat:@"%@人",model.count];
    
    NSInteger i = model.count.integerValue - model.preCount.integerValue;
    
    if (i > 0) {
        [self.changeButton setImage:Image(self.changeImageArray[0]) forState:UIControlStateNormal];
        [self.changeButton setTitle:[NSString stringWithFormat:@"%@人",@(i)] forState:UIControlStateNormal];
        [self.changeButton setTitleColor:ColorWithHexString(@"6ede6e") forState:UIControlStateNormal];
    } else if (i < 0) {
        [self.changeButton setImage:Image(self.changeImageArray[1]) forState:UIControlStateNormal];
        [self.changeButton setTitle:[NSString stringWithFormat:@"%@人",@(model.preCount.integerValue - model.count.integerValue)] forState:UIControlStateNormal];
        [self.changeButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    } else {
        [self.changeButton setImage:Image(self.changeImageArray[2]) forState:UIControlStateNormal];
        [self.changeButton setTitle:@"0人" forState:UIControlStateNormal];
        [self.changeButton setTitleColor:ColorWithHexString(@"6ede6e") forState:UIControlStateNormal];
    }
    [self layoutIfNeeded];
    
}
-(void)layoutSubviews {
    [super layoutSubviews];
    
    self.lineLabel.frame = CGRectMake(w(26), self.height - 0.2, kScreenWidth - w(26), 0.2);
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
